﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using TuyenSinh.BLL;

namespace TuyenSinh.Admin
{
    public partial class School : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                #region Add New

                if (Session["Admin"] != null)
                {
                    DataTable dt = new DataTable();
                    BSchool bSchool = new BSchool();
                    dt = bSchool.GetRegion();


                    ddlRegionAdd.DataSource = dt;
                    ddlRegionAdd.DataValueField = "Id";
                    ddlRegionAdd.DataTextField = "Name";
                    ddlRegionAdd.DataBind();

                    ddlRegion.DataSource = dt;
                    ddlRegion.DataValueField = "Id";
                    ddlRegion.DataTextField = "Name";
                    ddlRegion.DataBind();

                    ddlRegion.Items.Insert(0, new ListItem("-- Chọn vùng --", "0"));
                    //ddlRegion.SelectedIndex = 0;


                    dt = bSchool.GetSchoolType();
                    ddlSchoolType.DataSource = dt;
                    ddlSchoolType.DataTextField = "Name";
                    ddlSchoolType.DataValueField = "Id";
                    ddlSchoolType.DataBind();

                    ddlType.DataSource = dt;
                    ddlType.DataTextField = "Name";
                    ddlType.DataValueField = "Id";
                    ddlType.DataBind();

                    ddlType.Items.Insert(0, new ListItem("-- Chọn loại --", "0"));
                    //ddlType.SelectedIndex = 0;


                    dt = bSchool.GetSchoolGroup();
                    ddlShoolGroup.DataSource = dt;
                    ddlShoolGroup.DataValueField = "Id";
                    ddlShoolGroup.DataTextField = "Name";
                    ddlShoolGroup.DataBind();

                    ddlGroup.DataSource = dt;
                    ddlGroup.DataValueField = "Id";
                    ddlGroup.DataTextField = "Name";
                    ddlGroup.DataBind();

                    ddlGroup.Items.Insert(0, new ListItem("-- Chọn nhóm --", "0"));
                    // ddlGroup.SelectedIndex = 0;

                    #endregion
                }
                else
                {
                    Response.Redirect("Login.aspx");
                }

            }

        }

        [WebMethod]
        public static string InsertSchool(string sName, int iRegionId, string sDesc, string sAddress, int sSchoolTypeId, int sSchoolGroupId, int isActive, int isDelete)
        {
            BSchool  bSchool = new BSchool();
            bool result = bSchool.InsertSchool(sName, iRegionId, sDesc, sAddress, sSchoolTypeId, sSchoolGroupId,isActive, isDelete);
            return result ? "1" : "0";
        }

        protected void btnSubmitCate_Click(object sender, EventArgs e)
        {

            #region Edit
            DataTable dt = new DataTable();
            BSchool bSchool = new BSchool();
            string sName = txtNameSerch.Text.Trim();
            int iRegion = Convert.ToInt32(ddlRegion.SelectedItem.Value);
            int iSchoolType = Convert.ToInt32(ddlType.SelectedValue);
            int iSchoolGroup = Convert.ToInt32(ddlGroup.SelectedValue);

            dt = bSchool.GetSchool(sName, iRegion,iSchoolType,iSchoolGroup,1,0);

            grvSchool.DataSource = dt;
            grvSchool.DataBind();

            #endregion
        }

        protected void grvImg_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            BSchool bSchool = new BSchool();
            DataTable dtRegion = new DataTable();
            dtRegion = bSchool.GetRegion();
            DataTable dtType = bSchool.GetSchoolType();
            DataTable dtGroup = bSchool.GetSchoolGroup();

            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                TextBox txtSchoolName = (TextBox)e.Row.FindControl("txtSchoolName");
                txtSchoolName.Text = DataBinder.Eval(e.Row.DataItem, "Name").ToString();

                TextBox txtDesc = (TextBox)e.Row.FindControl("txtDesc");
                txtDesc.Text = DataBinder.Eval(e.Row.DataItem, "Description").ToString();

                TextBox txtAddress = (TextBox)e.Row.FindControl("txtAddress");
                txtAddress.Text = DataBinder.Eval(e.Row.DataItem, "Address").ToString();

                DropDownList ddlSchoolTypeEdit = (DropDownList)e.Row.FindControl("ddlSchoolTypeEdit");
                ddlSchoolTypeEdit.DataSource = dtType;
                ddlSchoolTypeEdit.DataValueField = "Id";
                ddlSchoolTypeEdit.DataTextField = "Name";
                ddlSchoolTypeEdit.SelectedValue = DataBinder.Eval(e.Row.DataItem, "SchoolType").ToString();
                ddlSchoolTypeEdit.DataBind();

                DropDownList ddlRegionEdit = (DropDownList)e.Row.FindControl("ddlRegionEdit");
                ddlRegionEdit.DataSource = dtRegion;
                ddlRegionEdit.DataValueField = "Id";
                ddlRegionEdit.DataTextField = "Name";
                ddlRegionEdit.SelectedValue = DataBinder.Eval(e.Row.DataItem, "RegionId").ToString();

                ddlRegionEdit.DataBind();

                DropDownList ddlSchoolGroupEdit = (DropDownList)e.Row.FindControl("ddlSchoolGroupEdit");
                ddlSchoolGroupEdit.DataSource = dtGroup;
                ddlSchoolGroupEdit.DataValueField = "Id";
                ddlSchoolGroupEdit.DataTextField = "Name";
                ddlSchoolGroupEdit.SelectedValue = DataBinder.Eval(e.Row.DataItem, "SchoolGroupId").ToString();

                ddlSchoolGroupEdit.DataBind();
    

                CheckBox ckActive = (CheckBox)e.Row.FindControl("ckActive");
                ckActive.Checked = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "IsActive"));

                CheckBox ckDelete = (CheckBox)e.Row.FindControl("ckDelete");
                ckDelete.Checked = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "IsDelete"));

               

            }
        }

        protected void grvImg_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}